@extends('layout.master')

@section('title','游戏详情')

@section('css')
    @parent
    <link href="{{ asset('plugins/bower_components/summernote/dist/summernote.css') }}" rel="stylesheet"/>
    <!-- animation CSS -->
    <link href="{{ asset('css/animate.css') }}" rel="stylesheet">
    <link href="{{ asset('plugins/bower_components/icheck/skins/all.css') }}" rel="stylesheet">
    <link href="{{ asset('plugins/bower_components/bootstrap-switch/bootstrap-switch.min.css') }}" rel="stylesheet">
@endsection
@section('main')
    <div class="white-box">
        <form class="form-horizontal form-material" method="post" enctype="multipart/form-data">
            <div class="row">
                <input type="hidden" name="game_id" id="game-id" value="{{ $game_id }}">
                <div class="col-md-4 col-sm-4">
                    <div class="form-group">
                        <label class="col-md-12">奖品名称</label>
                        <div class="col-md-12">
                            <input type="text" name="name" id="name"
                                   class="form-control form-control-line" required>
                        </div>
                    </div>
                </div>


                <div class="col-md-4 col-sm-4">
                    <label class="col-md-12">奖品图</label>
                    <div class="col-md-12">
                        <div class="form-group ">
                            <input type="file" id="img-url" class="dropify" name="img_url"
                                   data-height="200px"/>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <label class="col-md-12">奖品简介</label>
                <div class="col-md-12 col-sm-12">
                    <textarea name="contents" class="form-control" id="summernote" cols="30"
                              rows="5"></textarea>
                </div>
            </div>
        </form>
        <div class="form-group">
            <button class="fcbtn btn btn-success btn-outline btn-1e waves-effect" id="publish">添加
            </button>
            {!! csrf_field() !!}
            <a class="fcbtn btn btn-info btn-outline btn-1e waves-effect"
               href="{{ url('/game/gameInfo') . '/' . $game_id }}">返回
            </a>
        </div>
    </div>

@endsection
@section('js')
    @parent
    <script src="{{ asset('js/myself.js') }}"></script>
    <script>
        $("#publish").click(function (e) {
            e.preventDefault();
            var game_id = $("#game-id").val();
            var name = $("#name").val();
            var contents = $("#summernote").val();
            var img = document.getElementById("img-url").files;


            swal({
                    title: "",
                    text: "确定添加么？",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "是的",
                    closeOnConfirm: false
                },
                function () {
                    if (!img.length) {
                        swal("请上传图片!");
                        return false;
                    }
                    if ((img[0].type).indexOf("image/") === -1) {
                        swal("请上传图片!");
                        return false;
                    }
                    var img_url = picajax(img[0]);
                    $.ajax({

                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        url: "/game/commodity/publish/" + game_id,
                        type: "POST",
                        data: {
                            name: name,
                            contents: contents,
                            img_url: img_url
                        },
                        error: function (e) {
                            if (e.readyState === 4 && e.status === 422) {
                                for (var i in e.responseJSON) {
                                    if (e.responseJSON[i] !== 'The given data was invalid.') {
                                        err = e.responseJSON[i];
                                        for (var j in err) {
                                            toasterror(err[j][0]);
                                        }
                                    }
                                }
                            }
                        },

                        success: function (data) {
                            if (data.status !== 1) {
                                swal(data.message);

                            } else {
                                swal(data.message, '', 'success');
                                $('.confirm').click(function () {
                                    location.href = '/game/gameInfo/' + game_id
                                });
                            }
                        }
                    })
                }
            );
        });
    </script>
@endsection